<?php
/*
 * program:	均源
 * author:	雨中漫步 & 上善若水 & qhxin
 * email:	qhxin@qhxin.com
 * date:	2014/11/11
 * description:	又是一年双十一，不能剁手，就写点东西吧~~~~~~
 */

defined('IN_SCHOOL') or die();

/*{TranslateTagBegin}*/
class model_atmsg extends base_model{

	//hook mod_atmsg_start.php
	private $table;

	function __construct() {
		//hook mod_atmsg_init_init_before.php
		parent::__construct();
		$this->table = 'atmsg';
		//hook mod_atmsg_init_init_end.php
	}
	
	public function atmsg_add($uid,$url = '') {
		$mid = $this->db->maxid($this->table.'-mid', '+1');
		//hook mod_atmsg_atmsg_add.php
		return $this->db->set( $this->table.'-mid-'.$mid, 
					array(	'mid'=>$mid,'uid'=>$uid,'url'=>$url,
							'isread'=>0,'time'=> date('Y-m-d H:i:s')));
	}
	
	public function atmsg_edit($mid ) {
		//hook mod_atmsg_atmsg_edit.php
		
		return $this->db->update( $this->table.'-mid-'.$mid,
				array('isread'=>1) );
	}
	
	public function atmsg_count_unread($uid) {
		//hook mod_atmsg_atmsg_count_unread.php
		return $this->db->index_count($this->table,
								array('uid'=>$uid,'isread'=>0));
	}
	
	public function atmsg_count_all($uid) {
		//hook mod_atmsg_atmsg_count_all.php
		return $this->db->index_count($this->table, array('uid'=>$uid));
	}
	
	public function atmsg_show($uid = 0) {
		//hook mod_atmsg_atmsg_show.php
		return $this->db->get($this->table."-uid-$uid-isread-0");
	}
	
	public function atmsg_list($uid = 0, $start = 0, $limit = 0 ,$order = -1) {
		$ret = array();
		if($uid < 1)
			return $ret;
		$this->db->update( $this->table.'-uid-'.$uid.'-isread-0',
				array('isread'=>1) );
		$arr = $this->db->index_fetch($this->table , 'mid',
							array('uid'=>$uid), array('mid'=> $order)
							, $start, $limit);
		//hook mod_atmsg_atmsg_list.php
		return $arr;
	}
	
	public function del_old_ten_atmsgs_after_one_mouth() {
		//hook mod_atmsg_del_old_ten_atmsgs_after_one_mouth.php
		$ltim = date('Y-m-d H:i:s',time()-2592000);
		$cond = array('time' => array('<'=>$ltim) );
		$arr = $this->db->index_fetch_id($this->table , 'mid',
							$cond, array('mid'=> -1), 0, 10);
		foreach($arr as $k=>$v) {
			$this->db->delete($v);
		}
	}
	
	//hook mod_atmsg_end.php

}
/*{TranslateTagEnd}*/

?>